package dao;

import model.PropertyFee;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PropertyFeeDao {
    public void addPropertyFee(PropertyFee fee) throws Exception {
        Connection conn = DBUtil.getConnection();
        String sql = "INSERT INTO property_fee (id, houseId, amount, paid, dueDate) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, fee.getId());
        ps.setString(2, fee.getHouseId());
        ps.setDouble(3, fee.getAmount());
        ps.setBoolean(4, fee.isPaid());
        ps.setLong(5, fee.getDueDate().getTime());
        ps.executeUpdate();
        ps.close();
        conn.close();
    }

    public List<PropertyFee> getAllPropertyFees() throws Exception {
        Connection conn = DBUtil.getConnection();
        String sql = "SELECT * FROM property_fee";
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        List<PropertyFee> list = new ArrayList<>();
        while (rs.next()) {
            list.add(new PropertyFee(
                rs.getInt("id"),
                rs.getString("houseId"),
                rs.getDouble("amount"),
                rs.getBoolean("paid"),
                new Date(rs.getLong("dueDate"))
            ));
        }
        rs.close();
        st.close();
        conn.close();
        return list;
    }
}
